package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.edu.dao.BaseDao;
import com.edu.dao.myFollowDao;
import com.edu.entity.userFollow;

public class myFollowDaoImpl implements myFollowDao {
	
	BaseDao bd = new BaseDao();
	
	// 修改SQL查询，匹配实体类属性
	@Override
	public List<userFollow> getfollows(int userId) {
	    List<userFollow> followUsers = new ArrayList<>();
	    Connection conn = null;
	    PreparedStatement ps = null;
	    ResultSet rs = null;
	    try {
	        conn = bd.getConn();
	        // 修改别名匹配实体类属性
	        String sql = "SELECT u.userid AS userId, u.username, u.headimg AS headImg, " +
	                     "u.address AS address, u.fannum AS fanNum, " +
	                     "u.sellernum AS sellerNum, u.favorable_rate AS favorableRate, " +
	                     "u.soldCount AS soldCount, u.replyRate AS replyRate " +
	                     "FROM myfollows mf " +
	                     "INNER JOIN user u ON mf.followid = u.userid " +
	                     "WHERE mf.userid = ?";
	        
	        ps = conn.prepareStatement(sql);
	        ps.setInt(1, userId);
	        rs = ps.executeQuery();
	        
	        while (rs.next()) {
	            userFollow user = new userFollow();
	            // 设置所有需要的属性
	            user.setUserId(rs.getInt("userId"));
	            user.setUsername(rs.getString("username"));
	            user.setHeadImg(rs.getString("headImg"));
	            user.setAddress(rs.getString("address"));
	            user.setFanNum(rs.getString("fanNum"));
	            user.setSellerNum(rs.getInt("sellerNum"));
	            user.setFavorableRate(rs.getDouble("favorableRate"));
	            user.setSoldCount(rs.getInt("soldCount"));
	            user.setReplyRate(rs.getDouble("replyRate"));
	            followUsers.add(user);
	        }
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally {
	        bd.closeAll(conn, ps, rs);
	    }
	    
	    return followUsers;
	}

	@Override
	public int getFollowsCount(int userId) {
		Connection conn = null;
	    PreparedStatement ps = null;
	    ResultSet rs = null;
	    int count = 0;
	    
	    try {
	        conn = bd.getConn();
	        String sql = "SELECT COUNT(*) FROM myfollows WHERE userid = ?";
	        ps = conn.prepareStatement(sql);
	        ps.setInt(1, userId);
	        rs = ps.executeQuery();
	        
	        if (rs.next()) {
	            count = rs.getInt(1);
	        }
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally {
	        // 关闭资源
	        bd.closeAll(conn, ps, rs);
	    }
	    
	    return count;
	}
	
	public boolean unfollowUser(int userId, int followId) {
        Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            conn = bd.getConn();
            String sql = "DELETE FROM myfollows WHERE userid = ? AND followid = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, userId);
            ps.setInt(2, followId);
            
            int rows = ps.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            bd.closeAll(conn, ps, null);
        }
    }

	@Override
	public int sqlAddFollow(int userId, int sellerId) {
		// TODO Auto-generated method stub
		String sql = "insert into myfollows(followid,userid) value(?,?)";
		Object[] obj = {sellerId,userId};
		
		return bd.executeUpdate(sql, obj);
	}

	@Override
	public userFollow sqlFindFollow(int userId, int sellerId) {
		// TODO Auto-generated method stub
		String sql = "select * from myfollows where followid = ? and userid = ?";
		Connection conn = bd.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		userFollow uf = new userFollow();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, sellerId);
			ps.setInt(2, userId);
			System.out.println(ps);
			rs = ps.executeQuery();
			while(rs.next()) {
				uf.setUserId(rs.getInt("userid"));
			}
			return uf;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bd.closeAll(conn, ps, rs);
		}
		return null;
	}

	@Override
	public List<userFollow> getfans(int userid) {
		List<userFollow> followUsers = new ArrayList<>();
	    Connection conn = null;
	    PreparedStatement ps = null;
	    ResultSet rs = null;
	    try {
	        conn = bd.getConn();
	        // 修改别名匹配实体类属性
	        String sql = "SELECT u.userid AS userId, u.username, u.headimg AS headImg, " +
	                     "u.address AS address, u.fannum AS fanNum, " +
	                     "u.sellernum AS sellerNum, u.favorable_rate AS favorableRate, " +
	                     "u.soldCount AS soldCount, u.replyRate AS replyRate " +
	                     "FROM myfollows mf " +
	                     "INNER JOIN user u ON mf.userid = u.userid " +
	                     "WHERE mf.followid = ?";
	        
	        ps = conn.prepareStatement(sql);
	        ps.setInt(1, userid);
	        rs = ps.executeQuery();
	        
	        while (rs.next()) {
	            userFollow user = new userFollow();
	            // 设置所有需要的属性
	            user.setUserId(rs.getInt("userId"));
	            user.setUsername(rs.getString("username"));
	            user.setHeadImg(rs.getString("headImg"));
	            user.setAddress(rs.getString("address"));
	            user.setFanNum(rs.getString("fanNum"));
	            user.setSellerNum(rs.getInt("sellerNum"));
	            user.setFavorableRate(rs.getDouble("favorableRate"));
	            user.setSoldCount(rs.getInt("soldCount"));
	            user.setReplyRate(rs.getDouble("replyRate"));
	            followUsers.add(user);
	        }
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally {
	        bd.closeAll(conn, ps, rs);
	    }
	    
	    return followUsers;
	}
}


